<?php

/**
 * +--------------------------------------------------------+
 * @Created by PhpStorm.
 * @Category settingConfigTabService.php
 * @Depiction:
 * Author: hwy
 * Date: 2021/2/20  10:55
 * +--------------------------------------------------------+
 */

namespace App\Services;

use App\Models\SettingConfigTab;
use App\Exceptions\ResponseException;
use Exception;

class SettingConfigTabService extends BaseService
{
    protected $setting_config_tab;

    /**
     * SettingConfigTabService constructor.
     * @noinspection PhpMissingParentConstructorInspection
     */
    public function __construct()
    {
        $this->setting_config_tab = new SettingConfigTab();
    }

    /**
     * @param $params
     * @return mixed
     * @Depiction
     * @Author hwy
     * @Date  2021/2/20  17:35
     */
    public function getOne($params)
    {
        return $this->setting_config_tab->query()->find($params);
    }

    public function getAll()
    {
        return $this->setting_config_tab->query()->get();
    }

    /**
     * @param $params
     * @return mixed
     * @Depiction
     * @Author hwy
     * @Date  2021/2/20  17:35
     */
    public function getList($params)
    {
        $query   = $this->setting_config_tab->query();
        $perPage = $this->perPage($params);
        $query   = $this->paramsSet($params, $query);

        $list = $query->paginate($perPage)->toArray();

        return return_success_msg(ResponseException::REQUEST_SUCCESS_MESSAGE, $list);
    }

    /**
     * @param $params
     * @return mixed
     * @Depiction
     * @Author hwy
     * @Date  2021/2/20  17:35
     */
    public function addData($params)
    {
        $add_data           = $params->input();
        $add_data['status'] = $this->commonSetStatusValue($params->status);

        try {
            $add = $this->setting_config_tab->query()->create($add_data);
        } catch (Exception $e) {
            return return_error_msg($e->getMessage());
        }

        if ($add) {
            return return_success_msg(ResponseException::SUCCESS_ADD_MESSAGE);
        } else {
            return return_error_msg(ResponseException::ERROR_ADD_MESSAGE);
        }
    }

    /**
     * @param $params
     * @param $id
     * @return mixed
     * @Depiction
     * @Author hwy
     * @Date  2021/2/20  17:35
     */
    public function editData($params, $id)
    {
        $data = $this->getOne($id);

        if (empty($data)) {
            return return_error_msg(ResponseException::DATA_NOT_EXIST_MESSAGE);
        }

        $edit_data           = $params->input();
        $edit_data['status'] = $this->commonSetStatusValue($params->status);

        try {
            $edit = $data->update($edit_data);
        } catch (Exception $e) {
            $edit = false;
        }

        if ($edit) {
            return return_success_msg(ResponseException::SUCCESS_EDIT_MESSAGE);
        } else {
            return return_error_msg(ResponseException::ERROR_EDIT_MESSAGE);
        }

    }

    /**
     * @param $id
     * @return mixed
     * @Depiction
     * @Author hwy
     * @Date  2021/2/20  17:35
     */
    public function delData($id)
    {
        $data = $this->getOne($id);
        if (empty($data)) {
            return return_error_msg(ResponseException::DATA_NOT_EXIST_MESSAGE);
        }

        try {
            $del = $data->delete();
        } catch (Exception $e) {
            return return_error_msg($e->getMessage());
        }

        if ($del) {
            return return_success_msg(ResponseException::SUCCESS_DELETE_MESSAGE);
        } else {
            return return_error_msg(ResponseException::ERROR_DELETE_MESSAGE);
        }

    }

    /**
     * @param $id
     * @return mixed
     * @Depiction
     * @Author hwy
     * @Date  2021/2/20  17:35
     */
    public function setStatus($id)
    {
        $data = $this->getOne($id);

        if (empty($data)) {
            return return_error_msg(ResponseException::DATA_NOT_EXIST_MESSAGE);
        }

        if ($data->status == 1) {
            $data->status = 2;
        } else {
            $data->status = 1;
        }
        try {
            $edit = $data->save();
        } catch (Exception $e) {
            return return_error_msg($e->getMessage());
        }

        if ($edit) {
            return return_success_msg(ResponseException::SUCCESS_EDIT_MESSAGE);
        } else {
            return return_error_msg(ResponseException::ERROR_EDIT_MESSAGE);
        }
    }

}
